package net.jgrm.common.util;

import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据脱敏和加密使用示例
 *
 * @author service-common
 * @since 1.0-SNAPSHOT
 */
public class DataMaskingExample {

    /**
     * 手机号处理示例
     */
    public static void mobileExample() {
        System.out.println("=== 手机号处理示例 ===");

        String mobile = "13812345678";
        System.out.println("原始手机号: " + mobile);

        // 脱敏显示
        String maskedMobile = DataMaskingUtil.MobileUtil.mask(mobile);
        System.out.println("脱敏显示: " + maskedMobile);

        // AES加密
        String encryptedMobile = DataMaskingUtil.MobileUtil.encrypt(mobile);
        System.out.println("AES加密: " + encryptedMobile);

        // AES解密
        String decryptedMobile = DataMaskingUtil.MobileUtil.decrypt(encryptedMobile);
        System.out.println("AES解密: " + decryptedMobile);

        // MD5哈希（不可逆）
        String hashedMobile = DataMaskingUtil.MobileUtil.hash(mobile);
        System.out.println("MD5哈希: " + hashedMobile);

        System.out.println("解密验证: " + mobile.equals(decryptedMobile));
        System.out.println();
    }

    /**
     * 身份证处理示例
     */
    public static void idCardExample() {
        System.out.println("=== 身份证处理示例 ===");

        String idCard = "110101199003078515";
        System.out.println("原始身份证: " + idCard);

        // 验证格式
        boolean isValid = DataMaskingUtil.IdCardUtil.validate(idCard);
        System.out.println("格式验证: " + (isValid ? "有效" : "无效"));

        if (isValid) {
            // 脱敏显示
            String maskedIdCard = DataMaskingUtil.IdCardUtil.mask(idCard);
            System.out.println("脱敏显示: " + maskedIdCard);

            // 提取信息
            String birthday = DataMaskingUtil.IdCardUtil.getBirthday(idCard);
            int age = DataMaskingUtil.IdCardUtil.getAge(idCard);
            String gender = DataMaskingUtil.IdCardUtil.getGender(idCard);

            System.out.println("生日: " + birthday);
            System.out.println("年龄: " + age);
            System.out.println("性别: " + gender);

            // AES加密
            String encryptedIdCard = DataMaskingUtil.IdCardUtil.encrypt(idCard);
            System.out.println("AES加密: " + encryptedIdCard);

            // AES解密
            String decryptedIdCard = DataMaskingUtil.IdCardUtil.decrypt(encryptedIdCard);
            System.out.println("AES解密: " + decryptedIdCard);

            // MD5哈希（不可逆）
            String hashedIdCard = DataMaskingUtil.IdCardUtil.hash(idCard);
            System.out.println("MD5哈希: " + hashedIdCard);

            System.out.println("解密验证: " + idCard.equals(decryptedIdCard));
        }
        System.out.println();
    }

    /**
     * 银行卡处理示例
     */
    public static void bankCardExample() {
        System.out.println("=== 银行卡处理示例 ===");

        String bankCard = "6225880123456789";
        System.out.println("原始银行卡: " + bankCard);

        // 脱敏显示
        String maskedBankCard = DataMaskingUtil.BankCardUtil.mask(bankCard);
        System.out.println("脱敏显示: " + maskedBankCard);

        // AES加密
        String encryptedBankCard = DataMaskingUtil.BankCardUtil.encrypt(bankCard);
        System.out.println("AES加密: " + encryptedBankCard);

        // AES解密
        String decryptedBankCard = DataMaskingUtil.BankCardUtil.decrypt(encryptedBankCard);
        System.out.println("AES解密: " + decryptedBankCard);

        // MD5哈希（不可逆）
        String hashedBankCard = DataMaskingUtil.BankCardUtil.hash(bankCard);
        System.out.println("MD5哈希: " + hashedBankCard);

        System.out.println("解密验证: " + bankCard.equals(decryptedBankCard));
        System.out.println();
    }

    /**
     * 通用脱敏示例
     */
    public static void commonMaskExample() {
        System.out.println("=== 通用脱敏示例 ===");

        // 姓名脱敏
        String name = "张三丰";
        String maskedName = DataMaskingUtil.CommonUtil.maskName(name);
        System.out.println("姓名脱敏: " + name + " -> " + maskedName);

        // 邮箱脱敏
        String email = "zhangsan@example.com";
        String maskedEmail = DataMaskingUtil.CommonUtil.maskEmail(email);
        System.out.println("邮箱脱敏: " + email + " -> " + maskedEmail);

        // 地址脱敏
        String address = "北京市朝阳区三里屯街道";
        String maskedAddress = DataMaskingUtil.CommonUtil.maskAddress(address);
        System.out.println("地址脱敏: " + address + " -> " + maskedAddress);

        // 密码脱敏
        String password = "MyPassword123";
        String maskedPassword = DataMaskingUtil.CommonUtil.maskPassword(password);
        System.out.println("密码脱敏: " + password + " -> " + maskedPassword);

        // 自定义脱敏
        String customStr = "1234567890";
        String customMasked = DataMaskingUtil.CommonUtil.customMask(customStr, 2, 2, '#');
        System.out.println("自定义脱敏: " + customStr + " -> " + customMasked);

        System.out.println();
    }

    /**
     * 业务场景示例：用户信息处理
     */
    public static void userInfoExample() {
        System.out.println("=== 业务场景示例：用户信息处理 ===");

        // 模拟用户信息
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId", "USER001");
        userInfo.put("name", "张三");
        userInfo.put("mobile", "13812345678");
        userInfo.put("idCard", "110101199003078515");
        userInfo.put("bankCard", "6225880123456789");
        userInfo.put("email", "zhangsan@example.com");
        userInfo.put("address", "北京市朝阳区三里屯街道100号");

        System.out.println("原始用户信息:");
        System.out.println(JSONUtil.toJsonPrettyStr(userInfo));

        // 创建脱敏后的用户信息（用于前端显示）
        Map<String, Object> maskedUserInfo = new HashMap<>();
        maskedUserInfo.put("userId", userInfo.get("userId"));
        maskedUserInfo.put("name", DataMaskingUtil.CommonUtil.maskName((String) userInfo.get("name")));
        maskedUserInfo.put("mobile", DataMaskingUtil.MobileUtil.mask((String) userInfo.get("mobile")));
        maskedUserInfo.put("idCard", DataMaskingUtil.IdCardUtil.mask((String) userInfo.get("idCard")));
        maskedUserInfo.put("bankCard", DataMaskingUtil.BankCardUtil.mask((String) userInfo.get("bankCard")));
        maskedUserInfo.put("email", DataMaskingUtil.CommonUtil.maskEmail((String) userInfo.get("email")));
        maskedUserInfo.put("address", DataMaskingUtil.CommonUtil.maskAddress((String) userInfo.get("address")));

        System.out.println("\n脱敏后用户信息（前端显示）:");
        System.out.println(JSONUtil.toJsonPrettyStr(maskedUserInfo));

        // 创建加密后的用户信息（用于数据库存储）
        Map<String, Object> encryptedUserInfo = new HashMap<>();
        encryptedUserInfo.put("userId", userInfo.get("userId"));
        encryptedUserInfo.put("name", userInfo.get("name")); // 姓名通常不加密
        encryptedUserInfo.put("mobile", DataMaskingUtil.MobileUtil.encrypt((String) userInfo.get("mobile")));
        encryptedUserInfo.put("idCard", DataMaskingUtil.IdCardUtil.encrypt((String) userInfo.get("idCard")));
        encryptedUserInfo.put("bankCard", DataMaskingUtil.BankCardUtil.encrypt((String) userInfo.get("bankCard")));
        encryptedUserInfo.put("email", userInfo.get("email")); // 邮箱通常不加密
        encryptedUserInfo.put("address", userInfo.get("address")); // 地址根据需要决定是否加密

        System.out.println("\n加密后用户信息（数据库存储）:");
        System.out.println(JSONUtil.toJsonPrettyStr(encryptedUserInfo));

        // 解密验证
        System.out.println("\n解密验证:");
        String decryptedMobile = DataMaskingUtil.MobileUtil.decrypt((String) encryptedUserInfo.get("mobile"));
        String decryptedIdCard = DataMaskingUtil.IdCardUtil.decrypt((String) encryptedUserInfo.get("idCard"));
        String decryptedBankCard = DataMaskingUtil.BankCardUtil.decrypt((String) encryptedUserInfo.get("bankCard"));

        System.out.println("手机号解密验证: " + userInfo.get("mobile").equals(decryptedMobile));
        System.out.println("身份证解密验证: " + userInfo.get("idCard").equals(decryptedIdCard));
        System.out.println("银行卡解密验证: " + userInfo.get("bankCard").equals(decryptedBankCard));

        System.out.println();
    }

    /**
     * 自定义密钥示例
     */
    public static void customKeyExample() {
        System.out.println("=== 自定义密钥示例 ===");

        String mobile = "13812345678";
        String customKey = "MyCustomKey54321"; // 16位自定义密钥

        System.out.println("原始手机号: " + mobile);
        System.out.println("自定义密钥: " + customKey);

        // 使用自定义密钥加密
        String encrypted = DataMaskingUtil.MobileUtil.encrypt(mobile, customKey);
        System.out.println("自定义密钥加密: " + encrypted);

        // 使用自定义密钥解密
        String decrypted = DataMaskingUtil.MobileUtil.decrypt(encrypted, customKey);
        System.out.println("自定义密钥解密: " + decrypted);

        System.out.println("解密验证: " + mobile.equals(decrypted));
        System.out.println();
    }

    /**
     * 错误处理示例
     */
    public static void errorHandlingExample() {
        System.out.println("=== 错误处理示例 ===");

        // 空值处理
        System.out.println("空值脱敏: " + DataMaskingUtil.MobileUtil.mask(null));
        System.out.println("空值脱敏: " + DataMaskingUtil.MobileUtil.mask(""));
        System.out.println("空值脱敏: " + DataMaskingUtil.MobileUtil.mask("   "));

        // 无效身份证处理
        String invalidIdCard = "123456789";
        boolean isValid = DataMaskingUtil.IdCardUtil.validate(invalidIdCard);
        System.out.println("无效身份证验证: " + isValid);

        // 解密错误处理
        try {
            String wrongEncrypted = "invalid_encrypted_data";
            DataMaskingUtil.MobileUtil.decrypt(wrongEncrypted);
        } catch (Exception e) {
            System.out.println("解密错误处理: " + e.getMessage());
        }

        System.out.println();
    }

    /**
     * 主方法 - 运行所有示例
     */
    public static void main(String[] args) {
        System.out.println("Hutool 数据脱敏和加密处理示例");
        System.out.println("=====================================");

        mobileExample();
        idCardExample();
        bankCardExample();
        commonMaskExample();
        userInfoExample();
        customKeyExample();
        errorHandlingExample();

        System.out.println("所有示例执行完成！");
    }
}